################################################################################
# Project: Konado
# File: test_konadoscripts_generator.gd
# Author: DSOE1024
# Created: 2025-08-21
# Last Modified: 2025-08-21
# Description:
#    Konado脚本生成器单元测试脚本
################################################################################

extends GutTest

var generator: KonadoScriptsGenerator

func before_each():
	generator = KonadoScriptsGenerator.new()

func after_each():
	if generator:
		generator.free()

func test_generate_script_from_data_with_null_data():
	# 测试空数据情况
	var result = generator.generate_script_from_data(null)
	assert_eq(result, "", "空数据应返回空字符串")

func test_generate_script_from_data_with_empty_shot():
	# 测试空的shot数据
	var shot_data = double(DialogueShot).new()
	# 使用set_property来设置属性值
	shot_data.set("shot_id", "")
	shot_data.set("dialogs", [])
	
	var result = generator.generate_script_from_data(shot_data)
	assert_string_contains(result, "shot_id", "应包含shot_id行")
	assert_string_contains(result, "Generated by KonadoScriptsGenerator", "应包含生成注释")

func test_generate_dialog_line_label():
	# 测试标签类型
	var dialog = double(Dialogue).new()
	# 使用set_property设置属性
	dialog.set("dialog_type", Dialogue.Type.LABEL)
	dialog.set("label_notes", "这是一个标签")
	
	var result = generator._generate_dialog_line(dialog)
	assert_eq(result, "# 这是一个标签", "标签类型应正确生成")

func test_generate_dialog_line_ordinary_dialog():
	# 测试普通对话类型
	var dialog = double(Dialogue).new()
	dialog.set("dialog_type", Dialogue.Type.Ordinary_Dialog)
	dialog.set("character_id", "npc01")
	dialog.set("dialog_content", "你好，世界！")
	dialog.set("voice_id", "voice_001")
	
	var result = generator._generate_dialog_line(dialog)
	assert_eq(result, '"npc01" "你好，世界！" voice_001', "普通对话应正确生成")

func test_generate_dialog_line_ordinary_dialog_no_voice():
	# 测试普通对话类型（无语音）
	var dialog = double(Dialogue).new()
	dialog.set("dialog_type", Dialogue.Type.Ordinary_Dialog)
	dialog.set("character_id", "npc01")
	dialog.set("dialog_content", "你好，世界！")
	dialog.set("voice_id", "")
	
	var result = generator._generate_dialog_line(dialog)
	assert_eq(result, '"npc01" "你好，世界！"', "无语音的普通对话应正确生成")

func test_generate_dialog_line_background_with_effect():
	# 测试带效果的背景切换
	var dialog = double(Dialogue).new()
	dialog.set("dialog_type", Dialogue.Type.Switch_Background)
	dialog.set("background_image_name", "bg_forest")
	dialog.set("background_toggle_effects", ActingInterface.EffectsType.FadeInAndOut)
	
	var result = generator._generate_dialog_line(dialog)
	assert_eq(result, "background bg_forest fade", "带效果的背景切换应正确生成")

func test_generate_dialog_line_display_actor():
	# 测试显示角色
	var actor = double(DialogueActor).new()
	actor.set("character_name", "alice")
	actor.set("character_state", "happy")
	actor.set("actor_position", Vector2(100, 200))
	actor.set("actor_scale", 1.0)
	actor.set("actor_mirror", false)
	
	var dialog = double(Dialogue).new()
	dialog.set("dialog_type", Dialogue.Type.Display_Actor)
	dialog.set("show_actor", actor)
	
	var result = generator._generate_dialog_line(dialog)
	assert_eq(result, "actor show alice happy at 100 200 scale 1.0", "显示角色应正确生成")

func test_generate_dialog_line_show_choice():
	# 测试显示选择
	var choice1 = double(DialogueChoice).new()
	choice1.set("choice_text", "选择1")
	choice1.set("jump_tag", "tag1")
	
	
	var choice2 = double(DialogueChoice).new()
	choice2.set("choice_text", "选择2")
	choice2.set("jump_tag", "tag2")
	
	var dialog = double(Dialogue).new()
	dialog.set("dialog_type", Dialogue.Type.Show_Choice)
	# 如果直接传入Array[]类型会报错，需要使用Array[DialogueChoice]类型
	var choice_array: Array[DialogueChoice] = [choice1, choice2]
	dialog.set("choices", choice_array)
	
	var result = generator._generate_dialog_line(dialog)
	assert_eq(result, 'choice "选择1" tag1 "选择2" tag2', "选择应正确生成")

func test_generate_dialog_line_branch():
	# 测试分支
	var branch_dialog = double(Dialogue).new()
	branch_dialog.set("dialog_type", Dialogue.Type.Ordinary_Dialog)
	branch_dialog.set("character_id", "npc01")
	branch_dialog.set("dialog_content", "分支内对话")
	branch_dialog.set("voice_id", "")

	var branch_dialogue_array: Array[Dialogue] = [branch_dialog]
	
	var dialog = double(Dialogue).new()
	dialog.set("dialog_type", Dialogue.Type.Branch)
	dialog.set("branch_id", "branch_01")
	dialog.set("branch_dialogue", branch_dialogue_array)
	
	var result = generator._generate_dialog_line(dialog)
	assert_string_contains(result, "branch branch_01", "应包含分支声明")
	assert_string_contains(result, '    "npc01" "分支内对话"', "应包含缩进的分支内容")

func test_get_effect_name():
	# 测试效果名称获取
	assert_eq(generator._get_effect_name(ActingInterface.EffectsType.FadeInAndOut), "fade", "淡入淡出效果应返回'fade'")
	assert_eq(generator._get_effect_name(ActingInterface.EffectsType.None), "", "无效果应返回空字符串")

# 添加一个测试空数据的边缘情况
func test_generate_script_from_data_with_no_dialogs():
	var shot_data = double(DialogueShot).new()
	shot_data.set("shot_id", "test_shot")
	shot_data.set("dialogs", [])
	
	var result = generator.generate_script_from_data(shot_data)
	assert_string_contains(result, "shot_id test_shot", "应包含正确的shot_id")
	assert_string_contains(result, "Generated by KonadoScriptsGenerator", "应包含生成注释")
